#include "sdmmc.h"

void SD_ShowInfo(SD_HandleTypeDef hsd) {
    uint64_t capacity;
    HAL_SD_CardInfoTypeDef SDCardInfo;
    HAL_SD_CardCIDTypeDef SDCard_CID;
    HAL_SD_GetCardCID(&hsd, &SDCard_CID);
    HAL_SD_GetCardInfo(&hsd, &SDCardInfo);
    capacity = (uint64_t) (SDCardInfo.LogBlockNbr) * (uint64_t) (SDCardInfo.LogBlockSize);
    capacity = capacity >> 20;
}
/*
 * @函数：SD卡写数据
 * @参数：Data数据
 * @参数：SD卡块地址
 * @参数：SD卡块大小
 * @返回值：状态-1超时
 */
int8_t SD_Write(SD_HandleTypeDef hsd, uint8_t *Data, uint32_t Addr, uint32_t Len) {
    uint32_t timeout=100000;
    HAL_SD_WriteBlocks(&hsd,Data,Addr,Len,100000);
    while(HAL_SD_GetCardState(&hsd)!=HAL_SD_CARD_TRANSFER){
        if(timeout--==0){
            return -1;
        }
    }
    return 0;
}
/*
 * @函数：SD卡读数据，写数据大小必须为512整数倍
 * @参数：Data数据
 * @参数：SD卡块地址
 * @参数：SD卡块大小
 * @返回值：状态
 */
int8_t SD_Read(SD_HandleTypeDef hsd, uint8_t *Data, uint32_t Addr, uint32_t Len) {
    uint32_t timeout=100000;
    HAL_SD_ReadBlocks(&hsd,Data,Addr,Len,100000);
    while(HAL_SD_GetCardState(&hsd)!=HAL_SD_CARD_TRANSFER){
        if(timeout--==0){
            return -1;
        }
    }
    return 0;
}
/*
 * @函数：SD卡擦除数据
 * @参数：块起始地址
 * @参数：块结束地址
 * @状态
 */
int8_t SD_Erase(SD_HandleTypeDef hsd,uint32_t Start,uint32_t End){
    return HAL_SD_Erase(&hsd,Start,End);
}